package importantPort

import (
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"xkginweb/global"
	"xkginweb/model/entity/importantPort"
	"xkginweb/model/entity/importantPort/request"
)

// 定义importantPort的service提供importantPort的数据curd的操作

type ImportantPortService struct{}

// @author: hcl
// @function: CreateImportantPort
// @description: 创建重要端口
// @param: importantPort.QccImportantPort
// @return: err error

func (service *ImportantPortService) CreateImportantPort(importantPort *importantPort.QccImportantPort) (err error) {
	// 1： 获取数据的连接对象 如果执行成功err是nil，如果失败就把失败告诉
	err = global.KSD_DB.Create(importantPort).Error
	return err
}

//@author: hcl
//@function: UpdateImportantPort
//@description: 更新重要端口
//@param: importantPort.QccImportantPort
//@return: err error

func (service *ImportantPortService) UpdateImportantPort(importantPort *importantPort.QccImportantPort) (err error) {
	//err = global.KSD_DB.Save(importantPort).Error
	err = global.KSD_DB.Model(importantPort).Updates(importantPort).Error
	return err
}

// @author: hcl
// @function: DeleteImportantPort
// @description: 删除重要端口
// @param: importantPort.QccImportantPort
// @return: err error

func (service *ImportantPortService) DeleteImportantPort(importantPort *importantPort.QccImportantPort) (err error) {
	err = global.KSD_DB.Delete(&importantPort).Error
	return err
}

// @author: hcl
// @function: DeleteImportantPortById
// @description: 根据ID删除重要端口
// @param: uint
// @return: err error

func (service *ImportantPortService) DeleteImportantPortById(id uint) (err error) {
	var importantPort importantPort.QccImportantPort
	err = global.KSD_DB.Where("cooling_pool_id = ?", id).Delete(&importantPort).Error
	return err
}

// @author: hcl
// @function: GetImportantPort
// @description: 根据ID获取重要端口信息
// @param: id uint
// @return: importantPort *importantPort.QccImportantPort, err error

func (service *ImportantPortService) GetImportantPort(id uint) (importantPort *importantPort.QccImportantPort, err error) {
	err = global.KSD_DB.Where("important_port_id = ?", id).Preload("QccRoom", func(db *gorm.DB) *gorm.DB {
		return db.Table("qcc_room").Select("room_id,room_name,room_type")
	}).First(&importantPort).Error
	return
}

// 查询房间已绑定的重要端口

func (service *ImportantPortService) SelectRoomPort(roomId uint) (info []*importantPort.QccImportantPort, err error) {
	err = global.KSD_DB.Select("t2.* ").Table("qcc_room t1,qcc_important_port t2").
		Where("t1.room_id = ? AND t1.room_id = t2.room_id and t2.is_deleted = 0", roomId).Scan(&info).Error
	return info, err
}

// 房间绑定重要端口

func (service *ImportantPortService) UpdateRoomPort(basis []*importantPort.QccImportantPortList) (err error) {
	// 事务加持
	tx := global.KSD_DB.Begin()

	// 开始批量修改冷池的房间号
	err = tx.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "important_port_id"}},
		DoUpdates: clause.AssignmentColumns([]string{"room_id"}),
	}).Table("qcc_important_port").Omit("created_at", "updated_at", "is_deleted", "port_number", "type").Create(&basis).Error

	if err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadImportantPortPage
//@description: 分页获取重要端口列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *ImportantPortService) LoadImportantPortPage(info request.ImportantPortPageInfo) (list interface{}, total int64, err error) {
	// 获取分页的参数信息
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&importantPort.QccImportantPort{})

	// 准备切片帖子数组
	var ImportantPortList []importantPort.QccImportantPort

	//if info.Status != -1 {
	//	db = db.Where("status = ?", info.Status)
	//}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(port_number like ?  )", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 查询中枢
	err = db.Count(&total).Error
	if err != nil {
		return ImportantPortList, total, err
	} else {
		// 执行查询
		err = db.Limit(limit).Offset(offset).Preload("QccRoom", func(db *gorm.DB) *gorm.DB {
			return db.Table("qcc_room").Select("room_id,room_name,room_type")
		}).Find(&ImportantPortList).Error
	}

	// 结果返回
	return ImportantPortList, total, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadImportantPort
//@description: 获取重要端口列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *ImportantPortService) LoadImportantPort(info request.ImportantPortInfo) (list []*importantPort.QccImportantPortList, err error) {

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&importantPort.QccImportantPort{})

	// 准备切片帖子数组
	list = make([]*importantPort.QccImportantPortList, 1, 1)

	if info.RoomId != nil {
		db = db.Where("room_id = ?", info.RoomId)
	}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(cooling_pool_name like ?  )", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 查询中枢

	// 执行查询
	err = db.Table("qcc_important_port").Omit("created_at", "updated_at", "is_deleted", "room_id", "room_name", "room_type").Find(&list).Error

	// 结果返回
	return list, err
}
